package com.qf.fanwang.controller;

import com.qf.fanwang.dao.impl.UDaoImpl;
import com.qf.fanwang.pojo.User;
import com.qf.fanwang.service.UService;
import com.qf.fanwang.service.impl.UServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/u")
public class UController extends BaseController {
    private UService uService;

    public UService getuService() {
        return uService;
    }

    public void setuService(UService uService) {
        this.uService = uService;
    }

/*public void initlize(){
    ApplicationContext ac=new ClassPathXmlApplicationContext("application2.xml");
    UController bean = ac.getBean(UController.class);
}*/

  /*  private ApplicationContext ac=new ClassPathXmlApplicationContext("application2.xml");
    private UController bean = ac.getBean(UController.class);*/
    protected String show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext ac=new ClassPathXmlApplicationContext("application2.xml");
        UController bean = ac.getBean(UController.class);
        List<User> list = bean.uService.select();
        req.setAttribute("list",list);
        return "forward:usershow";
    }


    protected String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext ac=new ClassPathXmlApplicationContext("application2.xml");
        UController bean = ac.getBean(UController.class);
        String uname = req.getParameter("username");
        uname = new String(uname.getBytes("iso-8859-1"), "utf-8");
        String upass = req.getParameter("userpass");
        upass = new String(upass.getBytes("iso-8859-1"), "utf-8");
        String uemail = req.getParameter("useremail");
        uemail = new String(uemail.getBytes("iso-8859-1"), "utf-8");
        String ugender = req.getParameter("usersexy");// 取不到值，不知道怎么搞
        ugender = new String(ugender.getBytes("iso-8859-1"), "utf-8");
        String flag = req.getParameter("flag");
        int uflag = Integer.valueOf(flag);
        String role = req.getParameter("role");
        int urole = Integer.valueOf(role);
        String ucode = req.getParameter("code");
        ucode = new String(ucode.getBytes("iso-8859-1"), "utf-8");
        User user = new User(null, uname, upass, uemail, ugender, uflag, urole, ucode);
        bean.uService.uadd(user);
        return "redirect:u";

    }
}
